package com.yanggeng.demo.beginnerguidedemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.PoiItem;
import com.wlwl.os.listbottomsheetdialog.OnItemClickListener;
import com.yanggeng.demo.beginnerguidedemo.activity.MapNaviActivity;
import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.TestApplication;

import java.util.List;

/**
 * 地图搜索列表适配器
 * Created by yanggeng on 2017/9/22.
 */

public class LocationListAdapter extends RecyclerView.Adapter<LocationListAdapter.LocationViewHolder> {

    private Context context;
    private List<PoiItem> poiItems;
    private OnItemClickListener onItemClickListener;

    public LocationListAdapter(Context context, List<PoiItem> poiItems) {
        this.context = context;
        this.poiItems = poiItems;
    }

    @Override
    public LocationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LocationViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_location_item, parent, false));
    }

    @Override
    public void onBindViewHolder(LocationViewHolder holder, final int position) {
        final PoiItem poiItem = poiItems.get(position);
        holder.tvName.setText(poiItem.getTitle());
        holder.tvDistance.setText(AMapUtils.calculateLineDistance(TestApplication.getApplication().getCurrLatLng(),
                new LatLng(poiItem.getLatLonPoint().getLatitude(), poiItem.getLatLonPoint().getLongitude())) + "米");
        holder.tvAddress.setText(poiItem.getSnippet());
        holder.ivGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                PackageManagerUtil.toMapApp(context, poiItem.getLatLonPoint().getLatitude(), poiItem.getLatLonPoint().getLongitude(), poiItem.getTitle());
                Intent intent = new Intent(context, MapNaviActivity.class);
                intent.putExtra("startLatLng", TestApplication.getApplication().getCurrLatLng());
                intent.putExtra("endLatLng", new LatLng(poiItem.getLatLonPoint().getLatitude(), poiItem.getLatLonPoint().getLongitude()));
                context.startActivity(intent);
            }
        });
        holder.ll_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return poiItems.size();
    }

    public void notifyData(List<PoiItem> poiItems) {
        this.poiItems = poiItems;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class LocationViewHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvDistance;
        TextView tvAddress;
        ImageView ivGo;
        LinearLayout ll_location;

        public LocationViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvDistance = (TextView) itemView.findViewById(R.id.tv_distance);
            tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            ivGo = (ImageView) itemView.findViewById(R.id.iv_go);
            ll_location = (LinearLayout) itemView.findViewById(R.id.ll_location);
        }
    }

}
